#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver;
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re;
from pyquery import PyQuery as pq;
import pymongo;
from  config import *

# browser=webdriver.PhantomJS(service_args=SERVICE_ARGS);
browser = webdriver.Chrome()
# browser.set_window_size(1400,900)
wait = WebDriverWait(browser, 10);
client = pymongo.MongoClient(MONGO_URL);
MONGO_DB = "dzdp"
MONGO_TABLE = "food"
db = client[MONGO_DB]
from bs4 import BeautifulSoup


def getProducts():
    html = browser.page_source;
    soup = BeautifulSoup(html, 'lxml')
    items = soup.select("#shop-all-list > ul > li")
    for item in items:
        try:
            recom_food = []
            recom_tag = []

            tags = item.select(".comment-list span")
            for tag in tags:
                recom_tag.append(tag.getText())
            foods = item.select(".recommend a")
            for food in foods:
                recom_food.append(food.string)
            product = {
                "recom_food": recom_food,
                "shop_score": recom_tag,
                "shop_name": item.select(".txt .tit a")[0].attrs["title"],
                "price": item.select(".txt .comment .mean-price b")[0].string,
                "com_count": item.select(".txt .comment .review-num b")[0].string,
                "location": item.select(".txt .tag-addr .addr")[0].string
            }
        except Exception as e:
            print(e)
            continue
        print(product)
        saveToMongo(product)
        # saveToMongo(product);


def search():
    try:
        browser.get("http://www.dianping.com/hangzhou/food");
        input = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#J-search-input"))
        );
        click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#J-channel-bnt')));
        input.send_keys("美食");
        click.click()
        # browser.switch_to_default_content()
        browser.switch_to_window(browser.window_handles[1])

        # print(browser.page_source)
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,
                                                   '.content-wrap')));

        getProducts();

    except TimeoutException:
        print("search timeout")
        # return search();


def next_page(page):
    print(page)
    try:
        click = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,
                                                           'body > div.section.Fix.J-shop-search > div.content-wrap > div.shop-wrap > div.page > a.next')));
        click.click()
        browser.switch_to_window(browser.window_handles[1])
        wait.until(
            EC.text_to_be_present_in_element(
                (By.CSS_SELECTOR,
                 'body > div.section.Fix.J-shop-search > div.content-wrap > div.shop-wrap > div.page > a.cur'),
                str(page)
            )
        );
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,
                                                   '.content-wrap')));
        getProducts();
    except TimeoutException:
        print("next page timeout");
        next_page(page);


def main():
    search()
    for page in range(2, 51):
        next_page(page)

def saveToMongo(result):
    try:
        if db[MONGO_TABLE].insert(result):
            print("save result success");
    except:
        print("save mongo error")
if __name__ == '__main__':
    main()
